package com.learn.config;

import com.learn.web.CorsFilter;
import com.learn.web.FilterException;
import com.learn.web.FilterOne;
import com.learn.web.FilterTwo;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.Ordered;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

@Configuration
public class WebMvcConfig implements WebMvcConfigurer {

    @Bean
    public FilterRegistrationBean<CorsFilter> customCorsFilterRegistration() {
        FilterRegistrationBean<CorsFilter> registration = new FilterRegistrationBean<>();
        registration.setFilter(new CorsFilter());
        registration.setOrder(Ordered.HIGHEST_PRECEDENCE);
        return registration;
    }

    @Bean
    public FilterRegistrationBean<FilterException> customFilterExceptionRegistration() {
        FilterRegistrationBean<FilterException> registration = new FilterRegistrationBean<>();
        registration.setFilter(new FilterException());
        registration.setOrder(Ordered.HIGHEST_PRECEDENCE);
        return registration;
    }

    @Bean
    public FilterRegistrationBean<FilterOne> customFilterOneRegistration() {
        FilterRegistrationBean<FilterOne> registration = new FilterRegistrationBean<>();
        registration.setFilter(new FilterOne());
        registration.setOrder(Ordered.HIGHEST_PRECEDENCE);
        return registration;
    }

    @Bean
    public FilterRegistrationBean<FilterTwo> customFilterTwoRegistration() {
        FilterRegistrationBean<FilterTwo> registration = new FilterRegistrationBean<>();
        registration.setFilter(new FilterTwo());
        registration.setOrder(Ordered.HIGHEST_PRECEDENCE);
        return registration;
    }


}
